import Toast from '@vant/weapp/toast/toast';
import Dialog from '@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    checked: true,
    proList: [],
    oneList: [],
    twoList: [],
    searchText: '',
    show: false,
    school: '',
    deal: '',
    classify: '',
    price: '',
    price2: '',
    school1: false,
    school2: false,
    deal1: false,
    deal2: false,
    classify1: false,
    classify2: false,
    classify3: false,
    classify4: false,
    classify5: false,
    classify6: false,
  },
  showLeft() {
    this.setData({ show: true })
  },
  onClose() {
    this.setData({ show: false });
  },
  inputChange: function(e) {
    let value = this.validateNumber(e.detail.value)
    this.setData({price: value})
  },
  inputChange2: function(e) {
    let value = this.validateNumber(e.detail.value)
    this.setData({price2: value})
  },
  iptblur() {
    console.log('this.data.price', this.data.price, this.data.price2)
    if (this.data.price && this.data.price2) {
      if (parseInt(this.data.price)>=parseInt(this.data.price2)) {
        Toast('请输入正确的价格区间');
        this.setData({price: '',price2: ''})
      }
    }
  },
  sx() {
    Dialog.confirm({
      title: '温馨提示',
      message: '是否重置内容',
    })
      .then(() => {
        this.setData({
          price: '',
          price2: '',
          school: '',
          deal: '',
          classify: '',
          school1: false,
          school2: false,
          deal1: false,
          deal2: false,
          classify1: false,
          classify2: false,
          classify3: false,
          classify4: false,
          classify5: false,
          classify6: false,
        })
        this.getProductData()
      })
      .catch(() => {
        // on cancel
      });
  },
  // 过滤，只取数字
  validateNumber(val) {
    return val.replace(/\D/g, '')
  },
  schoolChange(e){
    this.setData({ school: e.detail.value})
  },
  dealChange(e){
    this.setData({ deal: e.detail.value})
  },
  classifyChange(e){
    this.setData({ classify: e.detail.value})
  },
  searchChange: function(e) {
		this.setData({ searchText: e.detail.value })
  },
  onChange({ detail }) {
    // 需要手动对 checked 状态进行更新
    console.log('detail', detail)
    this.setData({ checked: detail });
    this.getProductData()
  },
  toDetail(e) {
    console.log(e.currentTarget.dataset.id)
    let data = JSON.stringify(e.currentTarget.dataset.id)
    wx.navigateTo({
      url: `../../subpkg/detail/index?detail=${encodeURIComponent(data)}`,
    })
  },
  getProductData() {
    this.setData({show:false})
    console.log('2122', {
      proType: this.data.checked ? '卖出':'买入',
      search: this.data.searchText,
      price: this.data.price,
      price2: this.data.price2,
      school: this.data.school,
      deal: this.data.deal,
      classify: this.data.classify
    },)
    wx.cloud.callFunction({
      name: 'quickstartFunctions',
      data: {
        type: 'getProduct',
        openid: '',
        index: true,
        proType: this.data.checked ? '卖出':'买入',
        search: this.data.searchText,
        price: parseInt(this.data.price),
        price2: parseInt(this.data.price2),
        school: this.data.school,
        deal: this.data.deal,
        classify: this.data.classify
      }
    }).then((resp) => {
        let list = JSON.parse(JSON.stringify(resp.result.list)).reverse()
        const half = Math.ceil(list.length / 2);
        this.setData({
          proList:resp.result.list,
          oneList: list.splice(0, half),
          twoList: list.splice(-half)
        })
        Toast.success('更新成功');
        // console.log('proList', this.data.proList)
        // console.log('oneList', this.data.oneList)
        // console.log('twoList', this.data.twoList)
    }).catch((e) => {console.log('e', e)});
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (wx.getStorageSync('first')) {
      wx.navigateTo({
        url: '../../subpkg/home/index',
      })
    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getProductData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})